﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Text;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using EZCRS.HCMS.DataAcl;
using System.Collections;
using System.Globalization;
using System.Threading;
using EZCRS.HCMS.Bll;

namespace EZCRS.HCMS.Web.manage
{
    /// <summary>
    /// 基类
    /// </summary>
    public class ManageBase : System.Web.UI.Page
    {
        //当前登录
        /// <summary>
        /// propertyid
        /// </summary>
        public int BasPropertyId = 0;
        /// <summary>
        /// 酒店编号
        /// </summary>
        public int BasHotelId = 0;//酒店编号
        /// <summary>
        /// 集团编号
        /// </summary>
        public int BasGroupId = 0;//集团编号
        /// <summary>
        /// 管理员名称
        /// </summary>
        public string BasUserName = "amin";//登录用户登录名
        /// <summary>
        /// 管理员id
        /// </summary>
        public int BasUserId = 0;
        /// <summary>
        /// 管理员类别
        /// </summary>
        public int BasUserType = 3; //用户类型 3=超级管理员 1=酒店管理员  2=集团管理员 0=普通人员
        /// <summary>
        /// 管理员角色
        /// </summary>
        public int BasUserRole = 1;

        /// <summary>
        /// 角色类型 0=局部 1=全局
        /// </summary>
        public int BasRolType = 0;

        /// <summary>
        /// 公共表
        /// </summary>
        public DataTable tableList = null;

        /// <summary>
        /// 语言类别
        /// </summary>
        public int LanguageId = 0;

        //当前应用的机构信息 编码和类型
        public int CurrentOrgId = 0, CurrentOrgType = 0;  //0 集团   1酒店 3=超级
        public int CurrentGroupId = 0, CurrentGroupType = 3;//集团或者超级管理员

        public int CurrentUserHelpType = 1;

        /// <summary>
        /// 从新获取机构信息
        /// </summary>
        public void UpdateCurrentInfo()
        {
            if (BasHotelId != 0)
            {
                CurrentOrgId = BasHotelId;
                CurrentOrgType = 1;
            }
            else if (BasGroupId != 0)
            {
                CurrentOrgId = BasGroupId;
                CurrentOrgType = 0;

            }
            else
            {
                CurrentOrgId = 0;
                CurrentOrgType = 3;
            }

            if (BasGroupId != 0)
            {
                CurrentGroupType = 0;
                CurrentGroupId = BasGroupId;
            }

        }

        /// <summary>
        /// 页面初始化
        /// </summary>
        /// <param name="e"></param>
        protected override void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
            PageBase_Load();
        }


        private void PageBase_Load()
        {
            //处理登录状态
            string paths = Page.Request.ApplicationPath.Replace("/", string.Empty);
            if (paths != "")
                paths = "/" + paths;

            HttpCookie co1 = null;
            try
            {
                co1 = Request.Cookies["cms"];
            }
            catch { }
            if (co1 == null || co1.Value == "")
            {
                Response.Write(EZCRS.HCMS.Bll.CFun.AlertMeg("请先登录！", "", paths + "/login.aspx"));
                Response.End();
            }
            else
            {
                BasHotelId = Convert.ToInt32(co1.Values["h"]);
                BasGroupId = Convert.ToInt32(co1.Values["g"]);
                BasUserName = co1.Values["n"];
                BasUserId = Convert.ToInt32(co1.Values["i"]);
                BasUserType = Convert.ToInt32(co1.Values["t"]);
                BasRolType = Convert.ToInt32(co1.Values["r"]);
                BasPropertyId = (co1.Values["p"] != "") ? Convert.ToInt32(co1.Values["p"]) : 0;
                BasUserRole = Convert.ToInt32(EZCRS.HCMS.Bll.CFun.DESDecrypt(co1.Values["d"], EZCRS.HCMS.Bll.CFun.CookieKey));
                CurrentUserHelpType = Convert.ToInt32(co1.Values["oh"]);
            }

            Init_GridView();

            UpdateCurrentInfo();

            try
            {
                string CurrentLanguage = System.Web.HttpContext.Current.Request.Cookies["clang"].Value;
                if (CurrentLanguage == "")
                    CurrentLanguage = "zh-CN";//默认简体中文

                //多语言选择
                //switch (CurrentLanguage)
                //{
                //    case "en-US"://英文
                //        LanguageId = 1;
                //        break;
                //}
            }
            catch (Exception ex) { }
        }

        #region 检查用户对模块的权限
        /// <summary>
        /// 检查用户对模块的权限
        /// </summary>
        /// <param name="CCmoduleid">模块编号</param>
        protected void CheckModule(int CCmoduleid)
        {
            bool bol = true;
            string CCmodulelist = EZCRS.HCMS.Bll.CFun.GetCookies("hcms", "l");

            if (CCmodulelist != "")
            {
                CCmodulelist = EZCRS.HCMS.Bll.CFun.DESDecrypt(CCmodulelist, EZCRS.HCMS.Bll.CFun.CookieKey) + ",";
                if (CCmodulelist.IndexOf(CCmoduleid.ToString() + ",") < 0)
                {
                    bol = false;
                }
            }
            else
            {
                bol = true;
            }
            if (!bol)
            {
                EZCRS.HCMS.Bll.CFun.AlertMeg("您没有相应的权限！", "back", "");
                Response.End();
            }
            else
            {
                loadhelp(CCmoduleid);
            }
        }

        /// <summary>
        /// 调用帮助
        /// </summary>
        /// <param name="CCmouleid"></param>
        protected void loadhelp(int CCmoduleid)
        {
            string helplist = "";
            if (CurrentUserHelpType == 1)
            {//允许显示帮助
                helplist = new Bllhfg_module_help().getjson(CCmoduleid);
            }
            System.Web.UI.LiteralControl ltl = new LiteralControl();

            StringBuilder sb = new StringBuilder();
            sb.Append("<script language=javascript>");
            if (helplist != "")
            {
                sb.Append("var helpinfo=" + helplist + ";\r\n");

                sb.Append("  function loadhelp()\r\n");
                sb.Append("{\r\n");
                //sb.Append("//加载所需样式文件\r\n");
                sb.Append(" var head = document.getElementsByTagName('HEAD').item(0);\r\n");
                sb.Append(" var style = document.createElement('link');\r\n");
                sb.Append(" style.href = '../../style/pophelp.css';\r\n");
                sb.Append(" style.rel = 'stylesheet';\r\n");
                sb.Append(" style.type = 'text/css';\r\n");
                sb.Append("head.appendChild(style); \r\n");

                sb.Append(" var body1=document.getElementsByTagName('body').item(0);\r\n");
                //sb.Append("//建立遮罩层\r\n");
                sb.Append("var fliterpanel=document.createElement(\"div\");\r\n");
                sb.Append(" fliterpanel.setAttribute(\"id\",\"helpboxpagecover\");\r\n");
                sb.Append("body1.appendChild(fliterpanel);\r\n");

                //sb.Append("//建立内容层\r\n");
                sb.Append(" var showpanel=document.createElement(\"div\");\r\n");
                sb.Append(" showpanel.setAttribute(\"id\",\"helpbox\");\r\n");
                sb.Append(" showpanel.setAttribute(\"class\",\"rounder\");\r\n");
                sb.Append(" body1.appendChild(showpanel);\r\n");

                sb.Append(" showpanel.style.top=(document.documentElement.clientHeight/2-200)+'px';\r\n");
                sb.Append("showpanel.style.left=(document.body.clientWidth/2 -350)+'px';\r\n");
                sb.Append("var str=\"<b class=\\\"t1\\\"></b><b class=\\\"t2\\\"></b><b class=\\\"t3\\\"></b><b class=\\\"t4\\\"></b>\";\r\n");
                sb.Append("str+=\"<ul class=\\\"title\\\"><div class=\\\"infotitle\\\"><span class=\\\"b1\\\">功能提示</span> <span class=\\\"h7\\\">| Tips</span><input type=\\\"hidden\\\" id=\\\"helpboxstep\\\" value=\\\"0\\\"/><a href=\\\"#\\\" class=\\\"close\\\" onclick=\\\"javascript:closehelp();\\\"><img width=\\\"19px\\\" height=\\\"19px\\\"  src=\\\"/Themes/Standard/Images/loading_close.gif\\\" alt=\\\"Close\\\" ></a></div></ul>\";\r\n");
                //sb.Append("str+=\"</div>\";\r\n");
                // sb.Append("str+=\"<div class=\\\"tlt\\\"></div>\";\r\n");
                sb.Append("str+=\"<div class=\\\"rounder_panel detail\\\" id=\\\"showboxdetail\\\">\"+helpinfo[0]+\"</div>\";\r\n");
                sb.Append("str+=\"<div class=\\\"nav\\\" id=\\\"showboxnav\\\"> <a href=\\\"#\\\" onclick=\\\"javascript:helpstep('n');\\\">下一步</a> &nbsp;</div>\";\r\n");
                sb.Append("showpanel.innerHTML=str;\r\n");

                sb.Append("}\r\n");


                //ltl.Text = "<script language=\"javascript\" type=\"text/javascript\">loadhelp();</script>";
                //Page.Form.Controls.Add(ltl);
            }
            else
            {
                sb.Append("var helpinfo=\"\";");
            }
            sb.Append("</script>");
            ltl.Text = sb.ToString();
            Page.Header.Controls.Add(ltl);
        }

        #endregion


        #region css 文件
        protected override void CreateChildControls()
        {
            //完整CSS文件路径
            try
            {
                BasUserId = Convert.ToInt32(Request.Cookies["cms"].Values["i"]);
            }
            catch { }
            string[] hostTemp = Page.Request.ServerVariables["HTTP_HOST"].Split(':');
            string host = hostTemp[0];
            string port = Page.Request.ServerVariables["SERVER_PORT"];
            port = port.Equals("80") ? "/" : (":" + port + "/");
            string appPath = Page.Request.ApplicationPath.Replace("/", string.Empty);
            string cssUrl = string.IsNullOrEmpty(appPath) ? ("Themes/Standard/CSS/Style.css") : ("/Themes/Standard/CSS/Style.css");
            string jsUrl = string.IsNullOrEmpty(appPath) ? ("common/common.js") : ("/common/common.js");
            string jsquery = string.IsNullOrEmpty(appPath) ? ("common/jquery-1.2.6.min.js") : ("/common/jquery-1.2.6.min.js");
            //如果个性化设置过，则调用个性化配置

            if (EZCRS.HCMS.Bll.CFun.FileIsExist("/" + appPath + "/template/usercss/" + BasUserId.ToString() + ".css"))
                cssUrl = string.IsNullOrEmpty(appPath) ? ("template/usercss/" + BasUserId.ToString() + ".css") : ("/template/usercss/" + BasUserId.ToString() + ".css");

            string fullCssUrl = "http://" + host + port + appPath + cssUrl;
            string mainjsUrl = "http://" + host + port + appPath + jsUrl;
            string mainjquery = "Http://" + host + port + appPath + jsquery;

            Page.ClientScript.RegisterClientScriptBlock(typeof(String), "main_style_file", "<link rel=\"stylesheet\" type=\"text/css\" href=\"" + fullCssUrl + "\" />");

            Page.ClientScript.RegisterClientScriptBlock(typeof(String), "main_js", "<script language=javascript src=\"" + mainjsUrl + "\" ></script>");
            //Page.ClientScript.RegisterClientScriptBlock(typeof(String), "jquery_js", "<script language=javascript src=\"" + mainjquery + "\" ></script>");
            base.CreateChildControls();
        }
        #endregion

        #region gridview list
        //初始化gridview
        private void Init_GridView()
        {
            GridView gvList = (GridView)this.FindControl("gvList");
            if (!Page.IsPostBack)
            {
                if (gvList != null)
                {
                    //样式
                    gvList.HeaderStyle.CssClass = "HeadStyle";
                    gvList.PagerStyle.CssClass = "PagerStyle";
                    gvList.RowStyle.CssClass = "RowStyle";

                    //分页 每页显示记录条数
                    try
                    {
                        gvList.PageSize = Convert.ToInt32(ConfigurationManager.AppSettings["gvpagesize"]);
                    }
                    catch
                    {
                        gvList.PageSize = 20;
                    }
                    gvList.AllowPaging = true;
                }
            }
            else
            {
                if (gvList != null)
                    gvList.PageIndexChanging += new GridViewPageEventHandler(gvList_PageIndexChanging);
            }
        }


        private void gvListBase_ItemDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
        {
            GridView gvList = (GridView)FindControl("gvList");
            foreach (GridViewRow gvr in gvList.Rows)
                if (gvr.RowType == DataControlRowType.DataRow)
                {
                    gvr.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#B9CEEA';");
                    gvr.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor;");
                    if (gvr.FindControl("Delete") != null)
                        //((LinkButton)gvr.FindControl("Delete")).Attributes.Add("onclick", "return confirm('" + Resources.Site.SystemConfirmDelete + "');");
                    ((LinkButton)gvr.FindControl("Delete")).Attributes.Add("onclick", "return confirm('确认删除？');");
                }
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gvList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView gv = (GridView)FindControl("gvList");
            DataBindTableList();
            gv.PageIndex = e.NewPageIndex;
            SetDataBind();
        }

        /// <summary>
        /// 绑定分页数据
        /// </summary>
        protected virtual void DataBindTableList()
        { }

        #endregion


        #region 数据绑定
        /// <summary>
        /// 数据绑定
        /// </summary>
        protected virtual void SetDataBind()
        {
            if (tableList != null)
            {
                GridView gv = (GridView)FindControl("gvList");
                if (gv != null)
                {
                    gv.DataSource = tableList;
                    gv.DataBind();
                }
            }

        }


        /// <summary>
        /// 数据绑定
        /// </summary>
        protected virtual void SetDataBind(DataTable dt)
        {
            if (dt != null)
            {
                GridView dg = (GridView)FindControl("gvList");
                if (dg != null)
                {
                    dg.DataSource = dt;
                    dg.DataBind();
                }
            }

        }


        #endregion

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            GridView gv = (GridView)FindControl("gvList");
            if (gv != null)
            {
                gv.RowDataBound += new GridViewRowEventHandler(gvListBase_ItemDataBound);
            }
        }


        #region //首选语言
        public string CurrentLanguage = DataAcl.Common.GetCurrentLanguage();
        protected override void InitializeCulture()
        {
            String selectedLanguage = CurrentLanguage;
            UICulture = selectedLanguage;
            Culture = selectedLanguage;

            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(selectedLanguage);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(selectedLanguage);
            base.InitializeCulture();
        }
        #endregion
    }


}